Ext.namespace("MyPlan");

MyPlan.Planing = function() {
	this.winNewPlan = null;
	this.winModPlan = null;
	this.winViewPlan = null;
	this.winMsg=null;
	this.remind=null;
	thiz=this;
	
	this.store = new Ext.data.JsonStore({
	    url: Utils.formatURL('../myplan/tempPlan.form?timesnap=' + Date.parse(new Date())),
	    root: 'result',
	    fields: ['planId','auditorId','createId','createName','auditorName', 'planName','planType','startDate','endDate','planState']
	});
	/*this.stateStore=new Ext.data.JsonStore({
		url:'../dictionaries/listDictionaries.form?groupId=201&timesnap=' + Date.parse(new Date()),
		root:'dictionaries',
		autoDestory:false,
		fields:['value','name']
	});
	this.stateStore.load();
	this.renderState = function(value){
		//return Utils.getNameFromStore(value,thiz.stateStore);
	};*/
	this.renderState = function(value){
		return OMS.PLANSTATUS[value];
	};	
	this.renderType = function(value){
		return OMS.PLANTYPE[value];
	};
	this.renderExport=function(value){
		var url='../auditplan/exportPlan.form?planId='+value+'&timesnap=' + Date.parse(new Date());
		return '<a href="'+url+'" >导出</a>';
	}
	this.sm =  new Ext.grid.CheckboxSelectionModel();
	this.column = [
		  this.sm,
		  {header: '计划编号', sortable: true, align: 'left',hidden:true, dataIndex: 'planId'},
		  {header: '审核人ID', sortable: true, align: 'left',hidden:true, dataIndex: 'auditorId'},		  
		  {header: '创建人名称', sortable: true, align: 'left',hidden:true, dataIndex: 'createName'},
		  {header: '创建人ID', sortable: true, align: 'left',hidden:true, dataIndex: 'createId'},
	      {header: '计划名称', sortable: true, align: 'left', dataIndex: 'planName'},
	      {header: '计划类型', sortable: true, align: 'center', width: 20, dataIndex: 'planType',renderer:this.renderType},
	      {header: '开始时间', sortable: true, align: 'center', width: 20, dataIndex: 'startDate'},
	      {header: '结束时间', sortable: true, align: 'center', width: 20, dataIndex: 'endDate'},
	      {header: '审核人', sortable: true,width: 40, align: 'left',dataIndex: 'auditorName'},
	      {header: '计划状态', sortable: true, align: 'center', width: 20, dataIndex: 'planState',renderer:this.renderState},
	      {header: '导出',  align: 'center',width: 40, dataIndex: 'planId',renderer:this.renderExport}
	];
	
	this.btnNewPlan = new Ext.Button({
		text: '新建计划',
		iconCls: 'icon-newplan',
		scope: this,
		handler: this.onNewPlan
	});
	this.btnModifyPlan = new Ext.Button({
		text: '修改计划',
		iconCls: 'icon-submitplan',
		scope: this,
		handler: this.onModifyPlan
	});
	this.btnSubmitPlan = new Ext.Button({
		text: '提交计划',
		iconCls: 'icon-submitplan',
		scope: this,
		handler: this.onSubmitPlan
	});
	this.btnDeletePlan = new Ext.Button({
		text: '删除计划',
		iconCls: 'icon-submitplan',
		scope: this,
		handler: this.onDeletePlan
	});
	this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查找',
        tooltip: {text: '查找计划'},
        handler: this.queryPlan,
        scope: this
    });
	MyPlan.Planing.superclass.constructor.call(this, {
		title: '草稿箱',
        region: 'center',
        tbar: [this.btnNewPlan, this.btnModifyPlan, this.btnSubmitPlan, this.btnDeletePlan,'->',this.btnSearch],
        cm: new Ext.grid.ColumnModel(this.column),
        sm:this.sm,	
        bbar:new Ext.PagingToolbar({
			 pageSize: 15,
	         store: this.store,
	         displayInfo: true,
	         displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
	         emptyMsg: "没有记录..."
		}),
        viewConfig: { forceFit: true },
        listeners:{
      		rowdblclick : this.onViewPlan,
			//render:this.queryPlan,
			activate: this.queryPlan
       }
    });
};
Ext.extend(MyPlan.Planing, Ext.grid.GridPanel, {
	onNewPlan: function(btn, event) {
		if (this.winNewPlan == null) {
			this.winNewPlan = new MyPlan.New(this);
		}
		this.winNewPlan.initWin();
		this.winNewPlan.show();
	},
	onViewPlan:function(grid, rowIndex,e){		
		if (this.winViewPlan == null) {
			this.winViewPlan = new MyPlan.WeeklyView(this);
		}		
		this.winViewPlan.show();
		this.winViewPlan.initWin(grid.store.getAt(rowIndex).data);
	},
	onModifyPlan: function(btn, event) {
		if (this.winModPlan == null) {
			this.winModPlan = new MyPlan.Modify(this);
		}
		var records = this.getSelectionModel().getSelections();
		if(records.length==0){
			Utils.warn2('请选择一条计划！',180);
			return ;
		}else if(records.length>1){
			Utils.warn2('只能选择一条计划！',180);
			return;
		}
		this.winModPlan.show();
		this.winModPlan.initWin(records[0].data);		
	},
	onSubmitPlan: function(btn, event) {
		scope=this;
		if(this.winMsg==null)
			this.winMsg=new Message.Msg(function(){
				scope.showSubmit();
				//alert(scope.remind);
			},'上报后就不可修改，你确实要上报吗？');
		this.winMsg.show();		
	},
	showSubmit:function(){
		var url='../myplan/submitPlan.form?timesnap=' + Date.parse(new Date());
		this.remind=this.winMsg.txtRemind;	
		this.submitPlan(url);
	},
	submitPlan:function(url){
		var records = this.getSelectionModel().getSelections();
		if(records.length==0){
			Utils.warn2('请选择一条计划！',180);
			return ;
		}
		var ids='';
		for(var i=0;i<records.length;i++){
			ids+=records[i].data['planId']+',';
		}
		scope=this;
		Ext.Ajax.request({
		   url: url,
		   params: {remind:this.remind,ids:ids},
		   success: function(){
			Utils.info2();
		   	scope.queryPlan();
		   }
		});
	},	
	onDeletePlan: function(btn, event) {
		var url='../myplan/deletePlan.form?timesnap=' + Date.parse(new Date());
		scope=this;
		Utils.confirm2({
			msg:'删除后将不可恢复，你确实要删除吗?',
			width:250,
			fn:function(){
	       		scope.submitPlan(url)
	       }
	    });
	},
	queryPlan:function(btn,event){
		this.store.load({
			params:{
				start : 0,
				limit : 15
				}
		});
	}
});
